package imageviewer.beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Collections;
import java.util.Locale;
import java.util.Vector;
import java.io.IOException;
import java.net.URL;

import org.apache.log4j.Logger;

import imageviewer.domain.Camera;
import imageviewer.domain.MyDate;

import imageviewer.persistence.Database;

public class DaysBean extends BasicBean {

	private static Logger mLog = Logger.getLogger(DaysBean.class);

	Vector<MyDate> days;
	
	Vector<Camera> cameras;
	
	Long userId = null;

	public DaysBean(){
		
	}
	
	public DaysBean(BasicBean bean){
		super.setError(bean.isError());
		super.setUserMessage(bean.getUserMessage());
	}
	
	public Vector<MyDate> getDays() {
		return days;
	}

	public void setDays(Vector<MyDate> days) {
		this.days = days;
	}

	public Vector<Camera> getCameras() {
		return cameras;
	}

	public void setCameras(Vector<Camera> cameras) {
		this.cameras = cameras;
	}

	public void getAllDays() {
		try {
			Connection con = Database.getConnection();

			Statement stmt = con.createStatement();
			
			DateFormat formater = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);
			ResultSet rs = stmt
					.executeQuery("SELECT DISTINCT mydate FROM Picture ");
			days = new Vector<MyDate>();
			
			while (rs.next()) {
//				mLog.debug (formater.format(rs.getDate(1)) + " :::" + rs.getDate(1));
				days.add(new MyDate(formater.format(rs.getDate(1)), rs.getDate(1)));
			}
			rs = stmt
				.executeQuery("SELECT a.Name, a.Camera_Id, URL FROM Camera a, Users b, User_Camera c  " +
						"where a.Camera_Id = c.Camera_Id and c.User_Id = b.User_Id and b.User_Id=" + userId.longValue());
			cameras = new Vector<Camera>();
			while (rs.next()) {
				cameras.add(new Camera( new URL (rs.getString("URL")) ,rs.getString("Name"),rs.getLong("Camera_Id")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

}
